package com.fengzhi.crawler.jsoup.thread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.fengzhi.crawler.jsoup.Fetcher.ImgFetcher;
import com.fengzhi.crawler.jsoup.Fetcher.impl.ImgFetcherImpl;
import com.fengzhi.crawler.jsoup.parser.BasicParserImpl;
import com.fengzhi.crawler.jsoup.parser.impl.BasicParser;
import com.fengzhi.crawler.jsoup.queue.LinkQueue;

public class CrawlerLinkThread implements Runnable {

	private CountDownLatch latch;
	private String url;
	private String site;
	private String dir;

	public CrawlerLinkThread(CountDownLatch latch, String url, String site, String dir) {
		this.latch = latch;
		this.url = url;
		this.site = site;
		this.dir = dir;
	}

	public void load() {

		System.out.println(Thread.currentThread().getName() + " : " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS").format(new Date()));

		// 添加未访问链接
		LinkQueue.addUnVisitedUrl(url);

		try {
			// 判断是否为空
			while (!LinkQueue.unVisitedUrlIsEmpty()) {
				// 从队列取出链接
				url = LinkQueue.unVisitedUrlDeQueue();

				// 判断链接是否已经访问过
				if (!LinkQueue.getVisitedUrl().contains(url)) {
					// 添加到已访问set集合
					LinkQueue.addVisitedUrl(url);

					// 获取页面文本
					Document document = Jsoup.connect(url).timeout(1000 * 10).get();

					// 解析
					BasicParser parser = new BasicParserImpl();
					document = parser.denoiseForDoc(document);
					// 获取页面中所有的链接
					Set<String> urls = parser.getLinks(document);

					// 遍历链接并添加到未访问队列
					Iterator<String> linkIterator = urls.iterator();
					while (linkIterator.hasNext()) {
						String link = linkIterator.next();

						LinkQueue.addUnVisitedUrl(site + link);
					}

					Set<String> imgUrls = parser.getImgUrl(document);

					Iterator<String> imgIterator = imgUrls.iterator();
					ImgFetcher fetch = new ImgFetcherImpl();
					while (imgIterator.hasNext()) {
						String imgLink = imgIterator.next();

						fetch.fetch(imgLink, dir);
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println(Thread.currentThread().getName() + " : " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS").format(new Date()));
		latch.countDown();

	}

	@Override
	public void run() {
		load();
	}

}
